<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>运维功能——页面查询</title>
    <script src="/webjars/miniui/boot.js" type="text/javascript"></script>
    <script src="/static/js/common/common.js" type="text/javascript"></script>
    <link href="/static/css/resource.css" rel="stylesheet" type="text/css" />
    <script src="/static/js/crypto/crypto-js.js"></script>
    <style type="text/css">
        body {
            margin:0;
            padding:0;
            border:0;
            width:100%;
            height:100%;
            overflow:hidden;
        }
    </style>
</head>
<body>
    <div class="mini-splitter" style="width:100%;height:100%;" borderStyle="border:8px;">
        <!-- 左边树 -->
        <div size="310">
            <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;">
                <table class="form-table" align="center" style="width:100%">
                    <tr>
                        <td>
                            <input name="sqlType" id="sqlType" class="mini-combobox" style="width: 140px" value="0"
                                   textField="value" valueField="key" emptyText="请选择..." />
                            <a class="mini-button" id="sqlClick" iconCls="fa-search" plain="true" onclick="click()">查询</a>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="mini-fit" style="margin-top: 10px;margin-left: 10px">
                <form id="form1" method="post">
                    <div class="mini-fit">
                        <table class="form-table" align="center" style="width:100%">
                            <tr>
                                <td>
                                    <input name="selectSql" id="selectSql" class="mini-textarea" style="width:100%;height:650px;font-size:15px"
                                           emptyText="查询SQL，仅支持查询SQL..."  required="true" />
                                </td>
                            </tr>
                        </table>
                    </div>
                </form>
            </div>
        </div>
        <div>
            <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;">
                <table class="form-table" align="center" style="width:100%;height:33px">
                    <tr>
                        <td>
                            返回内容：
                        </td>
                    </tr>
                </table>
            </div>
            <div class="mini-fit">
                <div id="datagrid1" class="mini-datagrid" style="width: 100%; height: 100%;" ajaxType="get" multiSelect="false"
                     virtualScroll="false" showLoading="true" idField="id">
                    <input name="orgid" class="mini-hidden" style="width: 100%;" readonly />
                    <div property="columns">
                        <div headerAlign="center">返回内容</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

<script type="text/javascript">
    mini.parse();
    var form = new mini.Form("form1");

    $(function (){
        mini.get("sqlType").setData([{ key: 0, value: '本省' },{ key: 1, value: '司法部' }]);
    });

    function click(){
        form.validate();
        if (!form.isValid() || !checkSQL() || !errorSqlCount()) {
            return;
        }else {
            sqlPut();
        }
    }

    //必须存在
    function checkSQL(){
        var sql = mini.get("selectSql").getValue().toLowerCase();
        var mustExistCount = ['select','from',';'];
        if(sql.slice(-1)!==';'){
            mini.alert('SQL语句不合规，请查看是否缺少\"select、from、;\"等关键字');
            return false;
        }
        for(var i=0;i<mustExistCount.length;i++){
            if(sql.indexOf(mustExistCount[i])===-1){
                mini.alert('SQL语句不合规，请查看是否缺少\"select、from、;\"等关键字');
                return false;
            }
        }
        return true;
    }

    //判断语句中不能存在的内容
    function errorSqlCount(){
        var sql = mini.get("selectSql").getValue().toLowerCase();
        var errorSqlCount = ['insert','delete','update', '#','truncate','drop','\'\''];
        for(var i=0;i<errorSqlCount.length;i++){
            if(sql.indexOf(errorSqlCount[i])!==-1){
                mini.alert("语句中不可包含\"insert、delete、update\"等修改性关键字");
                return false;
            }
        }
        return true;
    }

    //查询sql
    function sqlPut(){
        mini.mask({
            el: document.body,
            cls: 'mini-mask-loading',
            html: '数据加载中…'
        });
        $.ajax({
            url: '/azbj/maintenance/getPageSelect',
            data: {
                data: mini.encode({
                    selectSql: mini.get("selectSql").getValue(),
                    sqlType: mini.get("sqlType").getValue()
                })},
            type: "POST",
            success: function (res) {
                mini.unmask();
                if(res.code===1){
                    //mini.get("returnContent").setValue(JSON.stringify(res.data));
                    createTable(res.data);
                }else {
                    mini.alert(res.data);
                }
            },
            error: function () {
                mini.unmask();
            }
        });
    }

    //生成表格
    function createTable(data){
        if(data.length===0){
            mini.alert("查询内容为空");
            return;
        }
        var nameArr = Object.keys(data[0]);
        var columns = [
            { type: "indexcolumn" }
        ];
        for(var item of nameArr){
            columns.push({
                field: item,
                header: item,
                headerAlign: 'center',
                autoescape: true
            });
        }
        var gridView = mini.get("datagrid1");
        gridView.set({
            url: '/azbj/maintenance/getPageSelect',
            ajaxOptions: {
                type: "POST",
                data: {
                    data: mini.encode({
                        selectSql: mini.get("selectSql").getValue(),
                        sqlType: mini.get("sqlType").getValue()
                    })
                }
            },
            columns: columns
        });
        gridView.setData(data);
        gridView.render(document.getElementById("datagrid1"));
    }

</script>
</html>